package lambda;

import java.util.Arrays;
import java.util.List;

public class HelloLambda {
    public static void main(String[] args) {
        // 传统方式：使用匿名内部类
        Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("传统方式实现Runnable");
            }
        };
        new Thread(runnable1).start();

        // Lambda方式：简化Runnable实现
        Runnable runnable2 = () -> System.out.println("Lambda实现Runnable");
        new Thread(runnable2).start();

        // 带参数的Lambda表达式
        List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry");

        // 传统for循环
        System.out.println("\n传统for循环：");
        for (int i = 0; i < fruits.size(); i++) {
            System.out.println(fruits.get(i));
        }

        // 使用Lambda表达式的forEach
        System.out.println("\nLambda forEach：");
        fruits.forEach(fruit -> System.out.println(fruit));

        // 更简洁的形式（方法引用）
        System.out.println("\n方法引用：");
        fruits.forEach(System.out::println);
    }
}
